using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesAdminExpenseLangTranslation : System.Web.UI.Page
{
    #region Variables 

    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected readonly int TotalColumns = 4;
    protected DataObj CurPageDataObj = new DataObj();
    protected GLClaimTypes ExpenseDetailList = null;

    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState["ResultTable"];
        }

        set
        {
            ViewState["ResultTable"] = value;
        }
    }
    #endregion

    #region UpdatedMessage
    protected string UpdatedMessage
    {
        get
        {
            return ViewState["UpdatedMessage"].ToString();
        }

        set
        {
            ViewState["UpdatedMessage"] = value;
        }
    }
    #endregion

    #region AccountCode
    protected string AccountCode
    {
        get
        {
            return ViewState["AccountCode"].ToString();
        }

        set
        {
            ViewState["AccountCode"] = value;
        }
    }
    #endregion

    #region ExpenseName
    protected string ExpenseName
    {
        get
        {
            return ViewState["ExpenseName"].ToString();
        }

        set
        {
            ViewState["ExpenseName"] = value;
        }
    }
    #endregion

    #region ExpenseNameInLocal
    protected string ExpenseNameInLocal
    {
        get
        {
            return ViewState["ExpenseNameInLocal"].ToString();
        }

        set
        {
            ViewState["ExpenseNameInLocal"] = value;
        }
    }
    #endregion

    #region ErrorMessage
    protected string ErrorMessage
    {
        get
        {
            return ViewState["ErrorMessage"].ToString();
        }

        set
        {
            ViewState["ErrorMessage"] = value;
        }
    }
    #endregion

    #endregion

    #region Events

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        // This action is needed only at the first load
        if (!IsPostBack)
        {
            //FRANK: set readonly for Financial Admin
            if (this.CurrentPrivateSession.CurrentLoggedOnUser.IsFinanceAdmin && !this.CurrentPrivateSession.CurrentLoggedOnUser.IsFinancialManager)
            {
                Button_Update.Visible = false;
            }

            // Page Componenegt
            this.PageComponentSetup();

            // Language
            this.SwitchLanguage();

            // load header
            this.LoadData();


        }

        // built table
        this.BuildTable();
        if (this.ResultTable != null)
        {
            DataRow[] ResultRows = this.ResultTable.Select("", this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue);

            foreach (DataRow ResultRow in ResultRows)
            {
                this.BuildRow(ResultRow);
            }
        }
    }
    #endregion

    #region DropDownList_ByLanguage_SelectedIndexChanged
    protected void DropDownList_ByLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadData();

        // built table
        this.Table_ClaimDetailGrid.Rows.Clear();
        this.BuildTable();        
        if (this.ResultTable != null)
        {
            foreach (DataRow ResultRow in this.ResultTable.Rows)
            {
                this.BuildRow(ResultRow);
            }
        }
    }
    #endregion

    #region DropDownList_SortBy_SelectedIndexChanged
    protected void DropDownList_SortBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ResultTable != null)
        {
            this.DataCollectInDataTable();

            DataRow[] ResultRows = this.ResultTable.Select("", this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue);

            this.Table_ClaimDetailGrid.Rows.Clear();
            this.BuildTable();
            foreach (DataRow ResultRow in ResultRows)
            {
                this.BuildRow(ResultRow);
            }
        }
    }
    #endregion

    #region DropDownList_SortOrder_SelectedIndexChanged
    protected void DropDownList_SortOrder_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }
    #endregion

    #region Button_Update_Click
    protected void Button_Update_Click(object sender, EventArgs e)
    {        
        using (GLClaimTypeManager CurManager = new GLClaimTypeManager(this.CurrentPrivateSession))
        {
            this.DataCollect();

            if (CurManager.UpdateClaimTypeInLocalLanguage(this.ExpenseDetailList, this.DropDownList_ByLanguage.SelectedValue))
            {
                // log down the history
                this.CurLogManager.LogMessage("[GonzalesAdminExpenseLangTranslation] : Expense Detail Language (" + this.DropDownList_ByLanguage.SelectedValue + ") updated", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);

                this.ShowMessage(this.UpdatedMessage);

                // load header
                this.LoadData();

                // built table
                this.Table_ClaimDetailGrid.Rows.Clear();
                this.BuildTable();
                if (this.ResultTable != null)
                {
                    DataRow[] ResultRows = this.ResultTable.Select("", this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue);

                    foreach (DataRow ResultRow in ResultRows)
                    {
                        this.BuildRow(ResultRow);
                    }
                }

            }
            else
            {
                string LogID = SwissArmy.UniqueID();
                this.CurLogManager.LogError(LogID, "[GonzalesAdminExpenseLangTranslation.Button_Update_Click] : Failed at CurManager.UpdateClaimTypeInLocalLanguage : " + CurManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                this.ShowErrorMessage(this.ErrorMessage + LogID);
            }
        }
    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        // setup language
        using (LanguageManager CurLanguageManager = new LanguageManager(this.CurrentPrivateSession))
        {
            MasterBaseCollection ResultList = CurLanguageManager.GetAllLanguageInOrder();
            if (ResultList != null)
            {
                for (int x = 0; x < ResultList.Count; x++)
                {
                    this.DropDownList_ByLanguage.Items.Add(new ListItem(ResultList[x].DisplayName, ResultList[x].InternalID));
                }

                this.DropDownList_ByLanguage.Items.FindByValue(this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID).Selected = true;
            }
            else
            {
                // display error

                this.CurLogManager.LogError("[GonzalesAdminExpenseLangTranslation.PageComponentSetup] : Failed on CurLanguageManager.GetAllLanguageInOrder() : " + CurLanguageManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);

            }
        }

    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Button_Update.Text = CurLanguageFactory.DisplayValue("Update");
                this.ErrorMessage = CurLanguageFactory.DisplayValue("ErrorMessage");
                this.UpdatedMessage = CurLanguageFactory.DisplayValue("UpdatedMessage");
                this.AccountCode = CurLanguageFactory.DisplayValue("AccountCode");
                this.ExpenseName = CurLanguageFactory.DisplayValue("ExpenseName");
                this.ExpenseNameInLocal = CurLanguageFactory.DisplayValue("ExpenseNameInLocal");

                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("AccountCode"), this.CurPageDataObj.Code));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ExpenseName"), this.CurPageDataObj.DisplayName));

                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ASC"), "ASC"));
                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("DESC"), "DESC"));
            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesMyClaims.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region BuildTable
    public void BuildTable()
    {
        TableRow HeaderRow = new TableRow();

        for (int x = 0; x < this.TotalColumns; x++)
        {
            HeaderRow.Cells.Add(new TableCell());
            HeaderRow.Cells[x].CssClass = "GridViewHeader";
        }

        HeaderRow.Cells[1].Text = this.AccountCode; HeaderRow.Cells[1].HorizontalAlign = HorizontalAlign.Center;
        HeaderRow.Cells[2].Text = this.ExpenseName; HeaderRow.Cells[2].HorizontalAlign = HorizontalAlign.Left;
        HeaderRow.Cells[3].Text = this.ExpenseNameInLocal; HeaderRow.Cells[3].HorizontalAlign = HorizontalAlign.Left;

        this.Table_ClaimDetailGrid.Rows.Add(HeaderRow);

    }
    #endregion

    #region BuildRow

    public void BuildRow(DataRow ResultRow)
    {
        TableRow NewTableRow = new TableRow();
        NewTableRow.ID = ResultRow[this.CurPageDataObj.InternalID].ToString();

        for (int x = 0; x < this.TotalColumns; x++)
        {
            // set default setting to all the row
            NewTableRow.Cells.Add(new TableCell());
            NewTableRow.Cells[x].CssClass = "GridViewItem";
            NewTableRow.Cells[x].HorizontalAlign = HorizontalAlign.Center;
        }

        // code
        NewTableRow.Cells[1].Text = ResultRow[this.CurPageDataObj.Code].ToString();
        NewTableRow.Cells[1].Width = new Unit(60);

        // actual name
        NewTableRow.Cells[2].Text = ResultRow[this.CurPageDataObj.ActualName].ToString();
        NewTableRow.Cells[2].HorizontalAlign = HorizontalAlign.Left;
        NewTableRow.Cells[2].Width = new Unit(500);

        // description
        TextBox TextBoxInLang = new TextBox();
        TextBoxInLang.ID = NewTableRow.ID + "_TextBoxInLang";
        TextBoxInLang.Text = ResultRow[this.CurPageDataObj.DisplayName].ToString();
        TextBoxInLang.CssClass = "DetailGridTextBox";

        NewTableRow.Cells[3].Controls.Add(TextBoxInLang);
        NewTableRow.Cells[3].HorizontalAlign = HorizontalAlign.Left;
        NewTableRow.Cells[3].Width = new Unit(500);

        this.Table_ClaimDetailGrid.Rows.Add(NewTableRow);
    }

    #endregion

    #region DataCollect

    protected void DataCollect()
    {
        this.ExpenseDetailList = new GLClaimTypes();

        for (int x = 1; x < this.Table_ClaimDetailGrid.Rows.Count; x++)
        {
            GLClaimType CurrentGLObj = new GLClaimType(
                this.Table_ClaimDetailGrid.Rows[x].ID,
                ((TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[3].Controls[0]).Text,
                this.Table_ClaimDetailGrid.Rows[x].Cells[1].Text);

            this.ExpenseDetailList.Add(CurrentGLObj);
        }
    }

    protected void DataCollectInDataTable()
    {
        if (this.ResultTable != null)
        {
            for (int x = 1; x < this.Table_ClaimDetailGrid.Rows.Count; x++)
            {
                for (int y = 0; y < this.ResultTable.Rows.Count; y++)
                {
                    if (this.ResultTable.Rows[y][this.CurPageDataObj.InternalID].ToString().CompareTo(this.Table_ClaimDetailGrid.Rows[x].ID) == 0)
                    {
                        // found
                        this.ResultTable.Rows[y][this.CurPageDataObj.DisplayName] = ((TextBox)this.Table_ClaimDetailGrid.Rows[x].Cells[3].Controls[0]).Text;
                    }
                }
            }
        }
    }

    #endregion

    #region ShowErrorMessage
    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "ErrorBox";
    }
    #endregion

    #region ShowMessage
    protected void ShowMessage(string Message)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = Message;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "MessageBox";
    }
    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {
        using (GLClaimTypeManager CurClaimTypeManager = new GLClaimTypeManager(this.CurrentPrivateSession))
        {

            MasterBaseCollection ResultObj = CurClaimTypeManager.GetAllClaimTypeInLang((new Language(this.DropDownList_ByLanguage.SelectedValue, this.DropDownList_ByLanguage.SelectedItem.Text)));

            if (ResultObj != null)
            {
                this.ResultTable = new DataTable();
                ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.InternalID, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.Code, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.ActualName, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(this.CurPageDataObj.DisplayName, Type.GetType("System.String")));

                DataRow RowObj = null;

                for (int x = 0; x < ResultObj.Count; x++)
                {
                    GLClaimType CurGLClaimType = (GLClaimType)ResultObj[x];

                    RowObj = ResultTable.NewRow();

                    RowObj[this.CurPageDataObj.InternalID] = CurGLClaimType.InternalID;
                    RowObj[this.CurPageDataObj.Code] = CurGLClaimType.GLCode;
                    RowObj[this.CurPageDataObj.ActualName] = CurGLClaimType.ActualName;
                    RowObj[this.CurPageDataObj.DisplayName] = CurGLClaimType.DisplayName;

                    ResultTable.Rows.Add(RowObj);
                }
            }
            else
            {
                this.CurLogManager.LogError("[GonzalesAdminExpenseLangTranslation.LoadData] : Failed at CurClaimTypeManager.GetActiveClaimTypeInLangByEntity] : " + CurClaimTypeManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
            }

        }
    }
    #endregion

    #endregion

    #region Class: DataObj

    protected class DataObj
    {
        public readonly string InternalID = "InternalID";
        public readonly string Code = "Code";
        public readonly string ActualName = "ActualName";
        public readonly string DisplayName = "DisplayName";
    }

    #endregion

}
